<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/blog/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="数据结构,读书笔记," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="广义表广义表是线性表的推广和扩充，在人工智能领域 中应用十分广泛我们把线性表定义为n(n≧0 )个元素a1, a2 ,…, an的有穷序列，该序列中的所有元素具有相同的 数据类型且只能是原子项(Atom)。所谓原子项可以是一 个数或一个结构，是指结构上不可再分的。若放松对元 素的这种限制，容许它们具有其自身结构，就产生了广义表的概念。  定义 广义表(Lists，又称为列表 )是由n(n ≧0)个">
<meta name="keywords" content="数据结构,读书笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="广义表">
<meta property="og:url" content="https://git.oschina.net/choichumgming/blog/2017/12/26/广义表/index.html">
<meta property="og:site_name" content="choichumgming&#39;s blog">
<meta property="og:description" content="广义表广义表是线性表的推广和扩充，在人工智能领域 中应用十分广泛我们把线性表定义为n(n≧0 )个元素a1, a2 ,…, an的有穷序列，该序列中的所有元素具有相同的 数据类型且只能是原子项(Atom)。所谓原子项可以是一 个数或一个结构，是指结构上不可再分的。若放松对元 素的这种限制，容许它们具有其自身结构，就产生了广义表的概念。  定义 广义表(Lists，又称为列表 )是由n(n ≧0)个">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-26T08:35:59.833Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="广义表">
<meta name="twitter:description" content="广义表广义表是线性表的推广和扩充，在人工智能领域 中应用十分广泛我们把线性表定义为n(n≧0 )个元素a1, a2 ,…, an的有穷序列，该序列中的所有元素具有相同的 数据类型且只能是原子项(Atom)。所谓原子项可以是一 个数或一个结构，是指结构上不可再分的。若放松对元 素的这种限制，容许它们具有其自身结构，就产生了广义表的概念。  定义 广义表(Lists，又称为列表 )是由n(n ≧0)个">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://git.oschina.net/choichumgming/blog/2017/12/26/广义表/"/>





  <title>广义表 | choichumgming's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">choichumgming's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">stay foolish,stay hard</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/blog/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      
        
        <li class="menu-item menu-item-board">
          <a href="/blog/board/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://git.oschina.net/choichumgming/blog/blog/2017/12/26/广义表/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="choichumgming">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="choichumgming's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">广义表</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-26T16:06:34+08:00">
                2017-12-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/blog/2017/12/26/广义表/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2017/12/26/广义表/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <h1 id="广义表"><a href="#广义表" class="headerlink" title="广义表"></a>广义表</h1><p>广义表是线性表的推广和扩充，在人工智能领域 中应用十分广泛<br>我们把线性表定义为n(n≧0 )个元素a1, a2 ,…, an的有穷序列，该序列中的所有元素具有<strong>相同的 数据类型且只能是原子项(Atom)</strong>。所谓<strong>原子项可以是一 个数或一个结构，是指结构上不可再分的</strong>。若放松对元 素的这种限制，容许它们<strong>具有其自身结构</strong>，就产生了广义表的概念。 </p>
<h2 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h2><p> 广义表(Lists，又称为列表 )是由<strong>n(n ≧0)个元 素组成的有穷序列</strong>： LS=(a1，a2，…，an) 其中ai或者是原子项，或者是一个广义表。LS是广义 表的名字，n为它的长度。若ai是广义表，则称为LS的 子表。<br>若广义表LS<strong>非空</strong>时：</p>
<ol>
<li>a1(表中第一个元素)称为表头；</li>
<li>其余元素组成的子表称为表尾；(a2，a3，…，an) </li>
<li>广义表中所包含的元素(包括原子和子表)的个数称 为表的长 度。</li>
<li>广义表中括号的最大层数称为表深 (度）</li>
</ol>
<h2 id="广义表的重要结论："><a href="#广义表的重要结论：" class="headerlink" title="广义表的重要结论："></a>广义表的重要结论：</h2><ol>
<li>广义表的元素可以是原子，也可以是子表，子表 的元素又可以是子表， …。即广义表是一个多层次 的结构。</li>
<li>广义表可以被其它广义表所共享，也可以共享其 它广义表。广义表共享其它广义表时通过表名引用。</li>
<li>广义表本身可以是一个递归表。</li>
<li>根据对表头、表尾的定义，任何一个非空广义表 的表头可以是原子，也可以是子表， 而表尾必定是 广义表。</li>
</ol>
<h2 id="广义表的存储结构"><a href="#广义表的存储结构" class="headerlink" title="广义表的存储结构"></a>广义表的存储结构</h2><p> 由于广义表中的数据元素具有不同的结构，通常 用链式存储结构表示，每个数据元素用一个结点表示。<br><strong>广义表中就有两类结点：</strong><br>一类是<strong>子表结点</strong>，用来表示广义表项，由标志域， 子表表头域，后继结点域组成;<br>另一类是<strong>原子结点</strong>，用来表示原子项，由标志域， 原子的值域组成，后继结点域组成</p>
<p><strong>广义表代码实现要点：</strong><br>需要创建两个类。一个是节点类，里面有三个域，一个是数据域、一个是地址域，指向子表、一个是地址域，指向后继节点 。<br><strong>节点类代码实现：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span></span>&#123;<span class="comment">//节点类</span></div><div class="line">    <span class="keyword">public</span> String data;<span class="comment">//数据域</span></div><div class="line">    <span class="keyword">public</span> GenList child;<span class="comment">//地址域，指向子表 </span></div><div class="line">    <span class="keyword">public</span> Node next;<span class="comment">//地址域，指向后继节点   </span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">(String data, GenList child, Node next)</span> </span>&#123;<span class="comment">//构造方法重载        </span></div><div class="line">        <span class="keyword">this</span>.data = data;</div><div class="line">        <span class="keyword">this</span>.child = child;</div><div class="line">        <span class="keyword">this</span>.next = next;</div><div class="line">    &#125;   </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">(String data)</span> </span>&#123;      </div><div class="line">        <span class="keyword">this</span>.data = data;</div><div class="line">        <span class="keyword">this</span>.child = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">this</span>.next = <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">()</span> </span>&#123;     </div><div class="line">        <span class="keyword">this</span>.data = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">this</span>.child = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">this</span>.next = <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">        <span class="keyword">return</span> data.toString();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>一个是子表类，里面只有一个数据域，存的是子表的引用，是一个节点类的元素。<br><strong>广义表类代码实现：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">GenList</span></span>&#123;<span class="comment">//广义表类</span></div><div class="line">        <span class="keyword">public</span> Node head;   </div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">GenList</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="comment">// TODO Auto-generated constructor stub</span></div><div class="line">            <span class="keyword">this</span>.head=<span class="keyword">new</span> Node();</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.toString(<span class="string">""</span>);</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">(String str)</span> </span>&#123;</div><div class="line">            str+=<span class="string">"("</span>;</div><div class="line">            <span class="keyword">for</span>(Node p=<span class="keyword">this</span>.head.next; p!=<span class="keyword">null</span>;p=p.next)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(p.child==<span class="keyword">null</span>)</div><div class="line">                    str+=p.data.toString();</div><div class="line">                <span class="keyword">else</span></div><div class="line">                    str+=p.child.toString();</div><div class="line">                <span class="keyword">if</span>(p.next!=<span class="keyword">null</span>)str+=<span class="string">","</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> str+=<span class="string">")"</span>;</div><div class="line">            </div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">用递归实现广义表的创建。</div><div class="line">代码实现（注意：i是<span class="keyword">static</span>类型）</div><div class="line"><span class="function"><span class="keyword">public</span>  GenList <span class="title">create</span><span class="params">(String gliststr)</span></span></div><div class="line"><span class="function">    </span>&#123;</div><div class="line">        i=<span class="number">0</span>;<span class="comment">//字符串当前位置为i;</span></div><div class="line">        <span class="keyword">return</span> createsub(gliststr);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">private</span> GenList <span class="title">createsub</span><span class="params">(String gliststr)</span> </span>&#123;</div><div class="line">        <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">        </div><div class="line">        i++;        <span class="comment">//跳过“（” </span></div><div class="line">        GenList list =  <span class="keyword">new</span> GenList(); <span class="comment">//构造空广义表，只有头结点</span></div><div class="line">        </div><div class="line">        Node p=list.head;           <span class="comment">//指向头结点</span></div><div class="line">        </div><div class="line">        <span class="keyword">while</span>(i&lt;gliststr.length())</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">char</span> ch=gliststr.charAt(i);</div><div class="line">            <span class="keyword">switch</span>(ch)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">case</span><span class="string">','</span>: i++;<span class="keyword">break</span>;</div><div class="line">                <span class="keyword">case</span><span class="string">'('</span>: </div><div class="line">                    &#123;</div><div class="line">                        p.next=<span class="keyword">new</span> Node();<span class="comment">//创建子表节点</span></div><div class="line">                        p=p.next;</div><div class="line">                        p.child=createsub(gliststr);<span class="keyword">break</span>;<span class="comment">//递归调用创建子表</span></div><div class="line">                    &#125;</div><div class="line">                <span class="keyword">case</span><span class="string">'#'</span>: </div><div class="line">                &#123;</div><div class="line">                    p.next=<span class="keyword">new</span> Node();<span class="comment">//创建子表节点</span></div><div class="line">                    p=p.next;</div><div class="line">                    p.child=<span class="keyword">new</span> GenList();<span class="keyword">break</span>;<span class="comment">//递归调用创建子表</span></div><div class="line">                &#125;</div><div class="line">                <span class="keyword">case</span><span class="string">')'</span>:i++;<span class="keyword">return</span> list;</div><div class="line">                <span class="keyword">default</span>:        <span class="comment">//用子字符串表示原子</span></div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">int</span> j=i+<span class="number">1</span>;</div><div class="line">                    ch=gliststr.charAt(j);</div><div class="line">                    <span class="keyword">while</span>(ch!=<span class="string">'('</span>&amp;&amp;ch!=<span class="string">','</span>&amp;&amp;ch!=<span class="string">')'</span>)    <span class="comment">//获得子串</span></div><div class="line">                        ch=gliststr.charAt(++j);</div><div class="line">                    p.next=<span class="keyword">new</span> Node(gliststr.substring(i, j)); <span class="comment">//创建节点</span></div><div class="line">                    p=p.next;</div><div class="line">                    i=j;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">    &#125;</div></pre></td></tr></table></figure></p>
<p><strong>完整代码实现：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.Scanner;</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Lists</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> Node = <span class="number">0</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> i=<span class="number">0</span>;</div><div class="line">    <span class="keyword">private</span>  Node head;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> depth=<span class="number">0</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> length=<span class="number">0</span>;</div><div class="line">    <span class="keyword">private</span> GenList list=<span class="keyword">null</span>;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Lists</span><span class="params">()</span> </span>&#123;</div><div class="line">        Scanner sc=<span class="keyword">new</span> Scanner(System.in);</div><div class="line">        String str=sc.next();</div><div class="line">        list=create(str);</div><div class="line">        System.out.println(list.toString());</div><div class="line">        length=Genlength(list);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span>  GenList <span class="title">create</span><span class="params">(String gliststr)</span></span></div><div class="line"><span class="function">    </span>&#123;</div><div class="line">        i=<span class="number">0</span>;<span class="comment">//字符串当前位置为i;</span></div><div class="line">        <span class="keyword">return</span> createsub(gliststr);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">Genlength</span><span class="params">(GenList list)</span></span>&#123;</div><div class="line">        Node p=list.head;</div><div class="line">        <span class="keyword">int</span> length=<span class="number">0</span>;</div><div class="line">        </div><div class="line">        <span class="keyword">while</span>(p!=<span class="keyword">null</span>)&#123; <span class="comment">//遍历广义表内的节点</span></div><div class="line">            length++;</div><div class="line">            p=p.next;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> length;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span>  <span class="title">Gendepth</span><span class="params">(Node p,<span class="keyword">int</span> len)</span></span>&#123;</div><div class="line">        <span class="keyword">while</span>((p)!=<span class="keyword">null</span>)<span class="comment">//先判断子表的孩子节点是否为空</span></div><div class="line">        &#123;</div><div class="line">            len++;<span class="comment">//不为空则+1</span></div><div class="line">            <span class="keyword">if</span>(p.child.head!=<span class="keyword">null</span>)&#123;<span class="comment">//如果不为空表则递归</span></div><div class="line">                len=len&gt;Gendepth(p.child.head,len+<span class="number">1</span>)?len:Gendepth(p.child.head,len+<span class="number">1</span>);<span class="comment">//每次递归回来都与当前最长的长度比较</span></div><div class="line">            &#125;</div><div class="line">            p=p.next;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="keyword">return</span> len;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">private</span> GenList <span class="title">createsub</span><span class="params">(String gliststr)</span> </span>&#123;</div><div class="line">        <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">        </div><div class="line">        i++;        <span class="comment">//跳过“（”</span></div><div class="line">        </div><div class="line">        GenList list =  <span class="keyword">new</span> GenList(); <span class="comment">//构造空广义表，只有头结点</span></div><div class="line">        </div><div class="line">        Node p=list.head;           <span class="comment">//指向头结点</span></div><div class="line">        </div><div class="line">        <span class="keyword">while</span>(i&lt;gliststr.length())</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">char</span> ch=gliststr.charAt(i);</div><div class="line">            <span class="keyword">switch</span>(ch)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">case</span><span class="string">','</span>: i++;<span class="keyword">break</span>;</div><div class="line">                <span class="keyword">case</span><span class="string">'('</span>: </div><div class="line">                    &#123;</div><div class="line">                        p.next=<span class="keyword">new</span> Node();<span class="comment">//创建子表节点</span></div><div class="line">                        p=p.next;</div><div class="line">                        p.child=createsub(gliststr);<span class="keyword">break</span>;<span class="comment">//递归调用创建子表</span></div><div class="line">                    &#125;</div><div class="line">                <span class="keyword">case</span><span class="string">'#'</span>: </div><div class="line">                &#123;</div><div class="line">                    p.next=<span class="keyword">new</span> Node();<span class="comment">//创建子表节点</span></div><div class="line">                    p=p.next;</div><div class="line">                    p.child=<span class="keyword">new</span> GenList();<span class="keyword">break</span>;<span class="comment">//递归调用创建子表</span></div><div class="line">                &#125;</div><div class="line">                <span class="keyword">case</span><span class="string">')'</span>:i++;<span class="keyword">return</span> list;</div><div class="line">                <span class="keyword">default</span>:        <span class="comment">//用子字符串表示原子</span></div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">int</span> j=i+<span class="number">1</span>;</div><div class="line">                    ch=gliststr.charAt(j);</div><div class="line">                    <span class="keyword">while</span>(ch!=<span class="string">'('</span>&amp;&amp;ch!=<span class="string">','</span>&amp;&amp;ch!=<span class="string">')'</span>)    <span class="comment">//获得子串</span></div><div class="line">                        ch=gliststr.charAt(++j);</div><div class="line">                    p.next=<span class="keyword">new</span> Node(gliststr.substring(i, j)); <span class="comment">//创建节点</span></div><div class="line">                    p=p.next;</div><div class="line">                    i=j;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">        Lists p=<span class="keyword">new</span> Lists ();</div><div class="line">    &#125;</div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">GenList</span></span>&#123;<span class="comment">//广义表类</span></div><div class="line">        <span class="keyword">public</span> Node head;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">GenList</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="comment">// TODO Auto-generated constructor stub</span></div><div class="line">            <span class="keyword">this</span>.head=<span class="keyword">new</span> Node();</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.toString(<span class="string">""</span>);</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">(String str)</span> </span>&#123;</div><div class="line">            str+=<span class="string">"("</span>;</div><div class="line">            <span class="keyword">for</span>(Node p=<span class="keyword">this</span>.head.next; p!=<span class="keyword">null</span>;p=p.next)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(p.child==<span class="keyword">null</span>)</div><div class="line">                    str+=p.data.toString();</div><div class="line">                <span class="keyword">else</span></div><div class="line">                    str+=p.child.toString();</div><div class="line">                <span class="keyword">if</span>(p.next!=<span class="keyword">null</span>)str+=<span class="string">","</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> str+=<span class="string">")"</span>;</div><div class="line">            </div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Node</span></span>&#123;<span class="comment">//节点类</span></div><div class="line">        <span class="keyword">public</span> String data;<span class="comment">//数据域</span></div><div class="line">        <span class="keyword">public</span> GenList child;<span class="comment">//地址域，指向子表</span></div><div class="line">        </div><div class="line">        <span class="keyword">public</span> Node next;<span class="comment">//地址域，指向后继节点</span></div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">(String data, GenList child, Node next)</span> </span>&#123;<span class="comment">//构造方法重载</span></div><div class="line">            </div><div class="line">            <span class="keyword">this</span>.data = data;</div><div class="line">            <span class="keyword">this</span>.child = child;</div><div class="line">            <span class="keyword">this</span>.next = next;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">(String data)</span> </span>&#123;</div><div class="line">            </div><div class="line">            <span class="keyword">this</span>.data = data;</div><div class="line">            <span class="keyword">this</span>.child = <span class="keyword">null</span>;</div><div class="line">            <span class="keyword">this</span>.next = <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">()</span> </span>&#123;</div><div class="line">            </div><div class="line">            <span class="keyword">this</span>.data = <span class="keyword">null</span>;</div><div class="line">            <span class="keyword">this</span>.child = <span class="keyword">null</span>;</div><div class="line">            <span class="keyword">this</span>.next = <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="comment">// TODO Auto-generated method stub</span></div><div class="line">            <span class="keyword">return</span> data.toString();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/数据结构/" rel="tag"># 数据结构</a>
          
            <a href="/blog/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/26/线性表/" rel="next" title="线性表">
                <i class="fa fa-chevron-left"></i> 线性表
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <!--PC版-->
<div id="SOHUCS" ></div>


    

  </div>
     <div class="comments" id="comments">
       
     </div>
   





        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.gif"
               alt="choichumgming" />
          <p class="site-author-name" itemprop="name">choichumgming</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="http://www.weibo.com/u/2558763801" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                    
                      微博
                    
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#广义表"><span class="nav-number">1.</span> <span class="nav-text">广义表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#定义"><span class="nav-number">1.1.</span> <span class="nav-text">定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#广义表的重要结论："><span class="nav-number">1.2.</span> <span class="nav-text">广义表的重要结论：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#广义表的存储结构"><span class="nav-number">1.3.</span> <span class="nav-text">广义表的存储结构</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">choichumgming</span>

  
</div>


  <div class="powered-by">
    由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
  </div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">
    主题 &mdash;
    <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
      NexT.Gemini
    </a>
  </div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cytnT5dky';
      var conf = '56c170b7352bd027682e3707e9eb5071';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  





  





  

  

  

  

  

  

</body>
</html>
